// 路由守卫
import router from './router'
import store from './store'
import 'nprogress/nprogress.css' // 引入进度条样式
import nprogress from 'nprogress/nprogress.js' // 引入进度条
const whiteList = ['/404', '/login']
router.beforeEach(async (to, from, next) => {
  nprogress.start()
  if (store.getters.token) {
    if (to.path === '/login') {
      next('/')
    } else {
      if (!store.getters.userInfo.name) {
        const res = await store.dispatch('user/getUserInfo')
        console.log(res)
      }
      next()
    }
  } else {
    if (whiteList.indexOf(to.path) > -1) {
      next()
    } else {
      next('/login')
    }
  }
  nprogress.done()
})
router.afterEach((to, from, next) => {
  nprogress.done()
})

// // 前置守卫
// const whilelist = ['/login', '/404']
// router.beforeEach((to, from, next) => {
//   nprogress.start() // 开启进度条
//   if (store.getters.token) {
//     if (to.path === '/login') {
//       next('/')
//     } else {
//       next()
//     }
//   } else {
//     if (whilelist.indexOf(to.path) > -1) {
//       next()
//     } else {
//       next('/login')
//     }
//   }
//   nprogress.done()
// })

// // 后置守卫
// router.afterEach((to, from, next) => {
//   nprogress.done()
// })